<?php

namespace App\Http\Middleware;

use App\CodeResponse;
use Closure;
use Illuminate\Http\Request;

class CheckAdminLogin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next)
    {

        // 用户是否登录检查
        if (!auth()->check()) {
            list($errno,$errmsg) = CodeResponse::UN_LOGIN;
            $ret = ['errno' => $errno, 'errmsg' =>$errmsg];
            exit(json_encode($ret));
        }
        //用户是否在其他地方登陆
        if (Session()->getId() != auth()->user()->last_session){
            list($errno,$errmsg) = CodeResponse::UN_LOGIN_LOCAL;
            $ret = ['errno' => $errno, 'errmsg' =>$errmsg];
            exit(json_encode($ret));
        }
//        dd($request->route());
        return $next($request);
    }
}
